package com.jagochan.imchat.module.user.controller;


import com.jagochan.imchat.module.common.domain.vo.resp.ApiResult;
import com.jagochan.imchat.module.common.interceptor.ContextInfoHolder;
import com.jagochan.imchat.module.common.utils.redissonlock.EnableRedissonLock;
import com.jagochan.imchat.module.user.domain.enums.IdempotentEnum;
import com.jagochan.imchat.module.user.domain.req.ModifyNameReq;
import com.jagochan.imchat.module.user.domain.req.WearBadgeReq;
import com.jagochan.imchat.module.user.domain.resp.BadgeResp;
import com.jagochan.imchat.module.user.domain.resp.UserInfoResp;
import com.jagochan.imchat.module.user.service.UserBackpackService;
import com.jagochan.imchat.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "用户信息")
@RestController
@RequestMapping("/capi/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("获取用户信息")
    @GetMapping("/userInfo")
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.getUserInfo(ContextInfoHolder.get().getUid()));
    }

    @ApiOperation("修改用户名")
    @PutMapping("/name")
    public ApiResult<?> modifyName(@RequestBody @Valid ModifyNameReq modifyNameReq) {
        userService.modifyName(ContextInfoHolder.get().getUid(), modifyNameReq.getName());
        return ApiResult.success();
    }

    @GetMapping("/badges")
    @ApiOperation("徽章列表")
    public ApiResult<List<BadgeResp>> badges() {
        List<BadgeResp> badgeList = userService.badges(ContextInfoHolder.get().getUid());
        return ApiResult.success(badgeList);
    }

    @PutMapping("/badge")
    @ApiOperation("佩戴徽章")
    public ApiResult<?> wearBadge(@Valid @RequestBody WearBadgeReq req) {
        userService.wearBadge(ContextInfoHolder.get().getUid(), req.getItemId());
        return ApiResult.success();
    }


    @Resource
    private UserBackpackService userBackpackService;

    @GetMapping("/test")
    public void test() {
        userBackpackService.requireItem(ContextInfoHolder.get().getUid(), 1L, IdempotentEnum.UID, "1");
    }

}

